<?php
declare (strict_types = 1);

namespace app\admin\controller;
use think\facade\Db;
use think\facade\View;

class Console extends Base
{
    public function index()
    {
        $today_stime = strtotime(date('Y-m-d',time()));
        $yesterday_stime = strtotime(date('Y-m-d',strtotime("-1 day")));
        //订单条件
        $map_order = [];
        $map_order[] = ['order_status','in',[1,2,3]];
        if($this->sid>0){
            $map_order[] = ['sid','=',$this->sid];
        }
        //订单数量
        $today_order_num = Db::name('order')->where('add_time','>=',$today_stime)->where($map_order)->count();
        $yesterday_order_num = Db::name('order')->where('add_time','>=',$yesterday_stime)->where('add_time','<',$today_stime)->where($map_order)->count();
        //订单金额
        $today_order_price = Db::name('order')->where('add_time','>=',$today_stime)->where($map_order)->sum('total_shop_price');
        $yesterday_order_price = Db::name('order')->where('add_time','>=',$yesterday_stime)->where('add_time','<',$today_stime)->where($map_order)->sum('total_shop_price');
        //商品条件
        $map_order = [];
        if($this->sid>0){
            $map_order[] = ['sid','=',$this->sid];
        }
        //商品数量
        $today_product_num = Db::name('goods')->where('on_time','>=',$today_stime)->where($map_order)->count();
        $total_product_num = Db::name('goods')->where($map_order)->count();
        //会员条件
        $map_user = [];
        $today_user_num = Db::name('users')->where('reg_time','>=',$today_stime)->where($map_user)->count();
        $yesterday_user_num = Db::name('users')->where('reg_time','>=',$yesterday_stime)->where('reg_time','<',$today_stime)->where($map_user)->count();;

        $rdata = [
            'today_order_num' => $today_order_num,
            'yesterday_order_num' => $yesterday_order_num,
            'today_order_price' => $today_order_price,
            'yesterday_order_price' => $yesterday_order_price,
            'today_product_num' => $today_product_num,
            'total_product_num' => $total_product_num,
            'today_user_num' => $today_user_num,
            'yesterday_user_num' => $yesterday_user_num,
        ];
        View::assign('rdata',$rdata);
        return View::fetch();
    }
    //获取数据图表
    public function getDataChart(){
        $monthEnd = time();
        $monthStart = strtotime(date('Y-m-1',time()));
        $days = 30;
        $dateArr = [];
        //订单条件
        $map_order = [];
        $map_order[] = ['order_status','in',[1,2,3]];
        if($this->sid>0){
            $map_order[] = ['sid','=',$this->sid];
        }
        //会员条件
        $map_user = [];
        for($i=0;$i<$days;$i++){
            $thisDayStart = $monthStart + $i * 86400;
            $thisDayEnd = $monthStart + ($i+1) * 86400;
            $dateArr[] = date('m-d',$thisDayStart);
            $thisday = date('Y-m-d',$thisDayStart);
            //订单金额
            $order_price = Db::name('order')->where('add_time','>=',$monthStart)->where('add_time','<',$monthEnd)->where($map_order)->count();
            //订单数量
            $order_num = Db::name('order')->where('add_time','>=',$monthStart)->where('add_time','<',$monthEnd)->where($map_order)->count();
            //会员数量
            $user_num = Db::name('users')->where('reg_time','>=',$monthStart)->where('reg_time','<',$monthEnd)->where($map_user)->count();;
            $orderChartMoney[] = $order_price>0?$order_price:0;
            $orderChartNum[] = $order_num>0?$order_num:0;
            $userChartNum[] = $user_num>0?$user_num:0;

        }
        $title=[];
        $series = [];

        $title[] = '订单金额';
        $series[] = [
            'name'=>'订单金额',
            'type'=>'line',
            'smooth'=>'true',
            'itemStyle'=>['color'=>'#ee6666'],
            'data'=>$orderChartMoney,
        ];
        $title[] = '订单数量';
        $series[] = [
            'name'=>'订单数量',
            'type'=>'line',
            'smooth'=>'true',
            'itemStyle'=>['color'=>'#66ccff'],
            'data'=>$orderChartNum,
        ];
        $title[] = '会员数量';
        $series[] = [
            'name'=>'会员数量',
            'type'=>'line',
            'smooth'=>'true',
            'itemStyle'=>['color'=>'#5470c6'],
            'data'=>$userChartNum,
        ];
        return json(['dateArr'=>$dateArr,'series'=>$series,'title'=>$title]);
    }
}
